<include file="default/User/layout" />
<block name="main">
    <div class="col-xs-12">
        <form class="form-horizontal login-form" action="#" method="">
            <div class="row login-title">
                <div class="col-xs-12 login-tab">
                    <a href="{:U('user/login')}" class="active">用户登录</a>
                </div>
            </div>
            <div class="row">
                <div class="one-loginin col-xs-7">
                    <div class="form-group">
                        <div class="col-xs-12">
                            <label class="control-label">邮箱/手机</label>
                            <input type="text" name="user" class="form-control" placeholder="请输入邮箱/手机">
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-xs-12">
                            <label class="control-label">密码</label>
                            <input type="password" name="pwd" class="form-control" placeholder="请输入密码">
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-xs-12">
                            <label class="control-label">验证码</label>
                            <div class="yzm-box">
                                <div class="pull-right">
                                    <img src="{:U('user/code')}" alt="" class="bk_validate_code">
                                </div>
                                <input type="text" name="code" class="form-control" placeholder="请输入验证码" style="width:215px;">
                            </div>

                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-xs-12">
                            <a class="btn btn-primary" href="javascript:;" onclick="onLoginClick();">授权并登录</a>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-xs-12">
                            <a href="{:U('user/retrieve')}" class="forgot-pwd">忘记密码？</a>
                        </div>
                    </div>
                </div>
                <div class="other-loginin col-xs-5">
                    <div class="row">
                        <div class="col-xs-12">
                            <p>还没有 白帽社区 帐号？</p>
                            <a href="{:U('user/register')}" class="btn btn-primary btn-block">立即注册</a>
                        </div>
                        <div class="col-xs-12 login-fr">
                            <p>您还可以使用其他方式登录</p>
                            <div class="login-way">
                                <a href="{:U('user/login_san')}"><i class="icon icon-qq"></i></a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <script type="text/javascript">


                $('.bk_validate_code').click(function () {
                    $(this).attr('src', '{:U("user/code")}?random=' + Math.random());
                });

                function onLoginClick() {
                    // 帐号
                    var user = $('input[name=user]').val();
                    if(user.length == 0) {
                        $('.bk_toptips').show();
                        $('.bk_toptips span').html('帐号不能为空');
                        setTimeout(function() {$('.bk_toptips').hide();}, 2000);
                        return;
                    }
                    if(user.indexOf('@') == -1) { //手机号
                        if(user.length != 11 || user[0] != 1) {
                            $('.bk_toptips').show();
                            $('.bk_toptips span').html('帐号格式不对!');
                            setTimeout(function() {$('.bk_toptips').hide();}, 2000);
                            return;
                        }
                    } else {
                        if(user.indexOf('.') == -1) {
                            $('.bk_toptips').show();
                            $('.bk_toptips span').html('帐号格式不对!');
                            setTimeout(function() {$('.bk_toptips').hide();}, 2000);
                            return;
                        }
                    }
                    // 密码
                    var pwd = $('input[name=pwd]').val();
                    if(pwd.length == 0) {
                        $('.bk_toptips').show();
                        $('.bk_toptips span').html('密码不能为空!');
                        setTimeout(function() {$('.bk_toptips').hide();}, 2000);
                        return;
                    }
                    if(pwd.length < 6) {
                        $('.bk_toptips').show();
                        $('.bk_toptips span').html('密码不能少于6位!');
                        setTimeout(function() {$('.bk_toptips').hide();}, 2000);
                        return;
                    }
                    // 验证码
                    var code = $('input[name=code]').val();
                    if(code.length == 0) {
                        $('.bk_toptips').show();
                        $('.bk_toptips span').html('验证码不能为空!');
                        setTimeout(function() {$('.bk_toptips').hide();}, 2000);
                        return;
                    }
                    if(code.length < 3) {
                        $('.bk_toptips').show();
                        $('.bk_toptips span').html('验证码不能少于3位!');
                        setTimeout(function() {$('.bk_toptips').hide();}, 2000);
                        return;
                    }

                    $.ajax({
                        type: "POST",
                        url: '{:U("user/login_c")}',
                        dataType: 'json',
                        cache: false,
                        data: {user: user, pwd: pwd, code:code},
                        success: function(data) {
                            if(data.status != 0) {
                                alert(data.message);
                                $('.bk_validate_code').attr('src', '/user/code?random=' + Math.random());
                                return;
                            }
                            location.href = "{:U('user/member')}";
                        },
                        error: function(xhr, status, error) {
                            console.log(xhr);
                            console.log(status);
                            console.log(error);
                        }
                    });
                }

            </script>
            <div class="bk_toptips"><span></span></div>
        </form>
    </div>
</block>
